import java.util.Stack;

public class Solution {
    public int calPoints(String[] ops) {
        Stack<Integer> stack = new Stack<>();
        for(String str : ops){
            if (str.equals("C")){
                stack.pop();
            }
            else if(str.equals("+")){
                int top = stack.pop();
                int newTop = top + stack.peek();
                stack.push(top);
                stack.push(newTop);
            }else if (str.equals("D")){
                stack.push(2*stack.peek());
            }else {
                stack.push(Integer.valueOf(str));
            }
        }
        int ret = 0;
        for(int a : stack){
            ret += a;
        }
        return ret;
    }
}
